class User {
  final int id;
  final String username;
  final String name;
  final String? email;
  final String role;
  final DateTime? lastLoginTime;

  User({
    required this.id,
    required this.username,
    required this.name,
    this.email,
    required this.role,
    this.lastLoginTime,
  });

  factory User.fromMap(Map<String, dynamic> map) {
    print('创建用户对象，输入数据: $map');
    print('name 字段类型: ${map['name'].runtimeType}');
    print('name 字段值: ${map['name']}');
    
    final name = map['name'] as String? ?? '未命名用户';
    print('最终使用的名称: $name');
    
    return User(
      id: map['id'] as int,
      username: map['username'] as String,
      name: name,
      email: map['email'] as String?,
      role: map['role'] as String? ?? 'user',
      lastLoginTime: map['last_login_time'] != null
          ? DateTime.tryParse(map['last_login_time'].toString())
          : null,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'username': username,
      'name': name,
      'email': email,
      'role': role,
      'last_login_time': lastLoginTime?.toIso8601String(),
    };
  }
} 